﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;

/// <summary>
///MyRoleProvider 的摘要说明
/// </summary>
public class MyRoleProvider:RoleProvider
{
    //返回用户的角色集合
    public override string[] GetRolesForUser(string username)
    {
        string query = "select USERROLE from BC_USER where USERNAME='" + username + "'";

        string[] tmp = new string[] { };
        string connStr = WebConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ConnectionString;
        OracleConnection conn = new OracleConnection(connStr);  

        OracleCommand comd = new OracleCommand(query,conn);

        conn.Open();

        using (OracleDataReader  dr = comd.ExecuteReader())
        {
            if (dr.Read())
            {
                tmp = dr["USERROLE"].ToString().Split(',');
            }
        }
        return tmp;
    }


    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        throw new NotImplementedException();
    }

    public override string[] GetAllRoles()
    {
        throw new NotImplementedException();
    }

    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        string connStr = WebConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ConnectionString;
        
        using (OracleConnection conn = new OracleConnection(connStr))
        {

            OracleCommand comm = new OracleCommand();

            comm.CommandText = "select * from BC_USER where USERNAME='" + username + "' and USERROLE='" + roleName + "'";   

            comm.Connection = conn;

            conn.Open();

            using (OracleDataReader dr = comm.ExecuteReader())
            {

                if (dr.HasRows)
                {
                    return true;
                }
                return false;
            }
        }
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override bool RoleExists(string roleName)
    {
        throw new NotImplementedException();
    }
}
